// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var validate = require("validate");
	var databind = require("databind");
	var css_detail_info = require("./clientManage_financeEvidentiary.css");
	var detail_info = require("./clientManage_details.js");
	var html_auditState_Yes = require("../client/clientManage_auditStateYes.html");
	var constant = require("constant");
	var html_auditState_No = require("../client/clientManage_auditStateNo.html");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var pageIndex1 = 1,
		pageSize1 = 10,
		totalPageCount1 = 1,
		totalRecordCount1 = 0; //分页初始化
	var enterpriseId = "";
	var businessLicenceImage = "";
	var certificateImage = "";
	var legalPersonFrontImage = "";
	var legalPersonBackImage = "";
	var qualificationsImage = "";
	var creditReportImage = "";
	var tools = require("tools");
	var css_situation = require("./clientManage_financeSituaion.css");
	var str = "",
		str1 = "",
		str2 = "";
	var init = function(page) {
		$(".nav_active").removeClass("nav_active1");
		$(".financeSituation").addClass("nav_active1");
		enterpriseId = page.params.enterpriseId || base.getUrlParam("enterpriseId");
		businessLicenceImage = page.params.businessLicenceImage || base.getUrlParam("businessLicenceImage");
		certificateImage = page.params.certificateImage || base.getUrlParam("certificateImage");
		legalPersonFrontImage = page.params.legalPersonFrontImage || base.getUrlParam("legalPersonFrontImage");
		legalPersonBackImage = page.params.legalPersonBackImage || base.getUrlParam("legalPersonBackImage");
		qualificationsImage = page.params.qualificationsImage || base.getUrlParam("qualificationsImage");
		creditReportImage = page.params.creditReportImage || base.getUrlParam("creditReportImage");

		databind.fillInput1($("#clientManage_financeSituation"), {
			action: "inquireEnterpriseFinanceSummaryPageList.json?enterpriseId=" + enterpriseId
		}, function(data){
//			if (data.datas[0] != null) {
//				$("#clientManage_financeSituation span[name=residueCreditLine]").html(data.datas[0].creditLine - data.datas[0].creditLineAlready);
//			}
			
			databind.fillInput1($("#clientManage_financeSituation .situationTab2 .situationDataContent"), {
				action: "inquireEnterpriseSummaryDetail.json?enterpriseId=" + enterpriseId
			}, function(data){
				if (data.datas[0] != null) {
					$("#clientManage_financeSituation span[name=residueCreditLine]").html(data.datas[0].creditLine - data.datas[0].creditLineAlready);
				}
			});
		});

		//历史融资
		$(".applyDetail1").unbind("click").click(function() {
			//   		location.href="../financing/financingManage.html?enterpriseId="+enterpriseId;
			router.route("financing/financeManageList.html?enterpriseId="+enterpriseId);
		});

		$("#clientManage_financeSituation .situationNav a").unbind("click").click(function() {
			$(this).parent().find("a").removeClass("situationNav-active");
			$(this).addClass("situationNav-active");
			$(".situationTab").addClass("none");
			$("." + $(this).attr("situationTab")).removeClass("none");
			
			tableInit();
		});

		//进行中的融资
		$(".applyDetail2").unbind("click").click(function() {
//			location.href = "../financing/financingManage.html?enterpriseId=" + enterpriseId;
			$(".situationNav a").removeClass("situationNav-active");
			$($(".situationNav a")[1]).addClass("situationNav-active");
			$(".situationTab").addClass("none");
			$("." + $($(".situationNav a")[1]).attr("situationTab")).removeClass("none");
		});

		//融资异常
		$(".applyDetail3").unbind("click").click(function() {
			location.href = "../financing/financingManage.html?abnormal=0&enterpriseId=" + enterpriseId;
		});
		
		inquireProcessFinanceApplicationPageList();
		inquireEnterpriseHistoryFinanceApplicationPageList();

		initEvent();
		router.init();
	};

	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		//点击企业基本信息
		$(".enterpriseInfo").on("click", function() {
			router.route("client/clientManage_details.html?enterpriseId=" + enterpriseId, true);
		});

		//证明材料
		$(".financeEvidentiary").on("click", function() {
			router.route("client/clientManage_financeEvidentiary.html?enterpriseId=" + enterpriseId +
				"&businessLicenceImage=" + businessLicenceImage + "&certificateImage=" + certificateImage +
				"&legalPersonFrontImage=" + legalPersonFrontImage + "&legalPersonBackImage=" + legalPersonBackImage +
				"&qualificationsImage=" + qualificationsImage + "&creditReportImage=" + creditReportImage, true);
		});
		
		//所属客户
		$(".financeBelongs").on("click", function() {
			router.route("client/clientManage_belongs.html?enterpriseId=" + enterpriseId);
		});

		$(".applyDetail").on("click", function() {
			location.href = "../client/clientApplicationList.html";
		});
		
		//点击实名认证
		$(".authenticationState").on("click", function() {
			router.route("client/clientManage_authentication.html?enterpriseId=" + enterpriseId, true);
		});

	};

	//查询进行中的融资列表
	var inquireProcessFinanceApplicationPageList = function(productType) {
		base.fnAjax({
			action: "inquireProcessFinanceApplicationPageList.json",
			oData: {
				enterpriseId: enterpriseId,
				pageIndex: pageIndex,
				pageSize: pageSize,
				productType: productType
					//				financeState:financeState
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					
					juicer.register("getValueByKey", constant.getValueByKey);
					juicer.register("formatCurrency", tools.formatCurrency);
					
					var tpl = $("#situationDataTableTpl").html();
					var html = juicer(tpl, data);
					
					$("#situationDataTableBody tbody").html(html);
					
					$("#situationDataTableBody").trigger("update");
					
					$(".showFinanceList").unbind("click").click(function(){
						pageIndex = 1;
						inquireProcessFinanceApplicationPageList($(this).parent().attr("productType"));
						$(".showAll").remove();
						$(this).parent().append('<a class="showAll" href="javascript:void(0);">查看全部</a>');
					});
					
					$("#clientManage_financeSituation").on("click", ".showAll", function(){
						pageIndex = 1;
						$(".showAll").remove();
						inquireProcessFinanceApplicationPageList(null);
					});

					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;

					base.pageControl(
					$(".situationTab2 .pagerCon"),
					totalRecordCount,
					data.pageIndex,
					data.pageSize,
					function(p) {
						pageIndex = p;
						inquireProcessFinanceApplicationPageList(productType);
						
//						tableInit();
					});
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};
	
	
	var tableInit = function() {
		  $("#situationDataTableBody").tablesorter({
		    widthFixed: false,
			theme: 'blue',
			showProcessing: false,
			 tabIndex: true,
			 sortMultiSortKey: "shiftKey",
		    sortResetKey: 'ctrlKey',
			ignoreCase: true,
			sortForce: null,
			resort:false,
			headers:{
				 0: {sorter: false},
				 1: {sorter: false},
      			 2: {sorter: false},
      			 3: {sorter: false},
      			 4: {sorter: false},
      			 5: {sorter: false},
      			 6: {sorter: false},
      			 7: {sorter: false},
      			 8: {sorter: false},
      			 9: {sorter: false},
      			 10: {sorter: false},
      			 11: {sorter: false},
      			 12: {sorter: false},
      			 13: {sorter: false},
      			 14: {sorter: false},
      			 15: {sorter: false},
      			 16: {sorter: false},
      			 17: {sorter: false}
				},
			headerTemplate : '{content} {icon}',
			widgets: [ 'zebra', 'scroller' ],
			 widgetOptions : {
		      scroller_height : 500,
		      // scroll tbody to top after sorting
		      scroller_upAfterSort: true,
		      // pop table header into view while scrolling up the page
		      scroller_jumpToHeader: true,
		      // In tablesorter v2.19.0 the scroll bar width is auto-detected
		      // add a value here to override the auto-detected setting
		      scroller_barWidth : null
		      // scroll_idPrefix was removed in v2.18.0
		      // scroller_idPrefix : 's_'
		    }
		  });
		  
			//事件
		  	$("#situationDataTableBody").bind("sortEnd",function(event) {
				var table = event.target;
				var columnSort       =  table.config.sortList[0][0];          									 //第几列排序，序号
				var columnSortName   =  $(table.config.headerList[table.config.sortList[0][0]]).text();          //第几列排序，名称
				var columnSortType   =  table.config.sortList[0][1];    	  									 //0:升序，1：降序
				//测试代码2：排序
			});
	};

	//查询已完成的融资列表
	var inquireEnterpriseHistoryFinanceApplicationPageList = function() {
		base.fnAjax({
			action: "inquireEnterpriseHistoryFinanceApplicationPageList.json",
			oData: {
				enterpriseId: enterpriseId,
				pageIndex: pageIndex1,
				pageSize: pageSize1
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#situationDataTableTpl1").html();
					var html = juicer(tpl, data);
					$("#situationDataTableBody1").html(html);

					totalPageCount1 = data.totalPageCount;
					totalRecordCount1 = data.totalRecordCount;

					base.pageControl(
					$(".situationTab3 .pagerCon"),
					totalRecordCount,
					data.pageIndex,
					data.pageSize,
					function(p) {
						pageIndex1 = p;
						inquireEnterpriseHistoryFinanceApplicationPageList();
					});
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};


	return {
		init: init
	};
});